var prefix = "/commanderList/manage";
$(function () {
    load();
});
$('#commanderListTable').on('load-success.bs.table', function (e, data) {
    if (data.total && !data.rows.length) {
        $('#commanderListTable').bootstrapTable('selectPage').bootstrapTable('refresh');
    }
});

function load() {
    $('#commanderListTable').bootstrapTable(
        {
            method: 'get', // 服务器数据的请求方式 get or post
            url: prefix + "/list", // 服务器数据的加载地址
            showRefresh: true,
            // showToggle : true,
            iconSize: 'outline',
            // toolbar: '#exampleToolbar',
            striped: true, // 设置为true会有隔行变色效果
            dataType: "json", // 服务器返回的数据类型
            pagination: true, // 设置为true会在底部显示分页条
            pageList: [10, 20, 50],
            // queryParamsType : "limit",
            // //设置为limit则会发送符合RESTFull格式的参数
            singleSelect: false, // 设置为true将禁止多选
            // contentType : "application/x-www-form-urlencoded",
            // //发送到服务器的数据编码类型
            pageSize: 10, // 如果设置了分页，每页数据条数
            pageNumber: 1, // 如果设置了分布，首页页码
            // search : true, // 是否显示搜索框
            showColumns: true, // 是否显示内容下拉框（选择显示的列）
            sidePagination: "server", // 设置在哪里进行分页，可选值为"client" 或者
            // "server"
            queryParams: function (params) {
                return {
                    limit: params.limit,
                    offset: params.offset,
                    order: 'desc',
                    follow: $.trim($("#follow").val()),
                    startDate: $.trim($("#startDate").val()),
                    endDate: $.trim($("#endDate").val()),
                    keyword: $.trim($('#keyword').val()),
                    centreId: $('#centreId').val(),
                    tnum: $('#tnum').val(),
                };
            },
            // //请求服务器数据时，你可以通过重写参数的方式添加一些额外的参数，例如 toolbar 中的参数 如果
            // queryParamsType = 'limit' ,返回参数必须包含
            // limit, offset, search, sort, order 否则, 需要包含:
            // pageSize, pageNumber, searchText, sortName,
            // sortOrder.
            // 返回false将会终止请求
            columns: [
                {
                    field: 'tXiaoqu',
                    title: '门店名称',
                    formatter: function (value, row, index) {
                        var xiao = '';
                        if (row.tXiaoqu != '' && row.tXiaoqu != null) {
                            xiao = '<span class="label label-primary">' + row.tXiaoqu + '</span><p></p>';
                        }
                        return xiao;
                    }
                },
                {
                    field: 'store',
                    title: '门头照',
                    formatter: function (value, row, index) {
                        return '<div id="main" style="width:160%;"><div style="width:10%" class="col-sm-2"><img style="width:50px;height:50px;" src=' + value + '></div>';
                    }
                },
                {
                    field: 'tAddress',
                    title: '门店详细地址'
                },
                {
                    field: 'realname',
                    title: '店长姓名'
                },
                {
                    field: 'mobile',
                    title: '手机号码'
                },
                {
                    field: 'createtime',
                    title: '注册日期'
                },

                {
                    field: 'id',
                    title: '操作',
                    align: 'center',
                    width: '22%',
                    formatter: function (value, row, index) {
                        var e = '<a style="margin-left: 20px;" class="btn btn-warning btn-sm ' + s_cancel_h + '" href="#" title="取消门店"  mce_href="#" onclick="cancelCommander(\''
                            + value + '\')"><i class="fa fa-remove"></i>取消门店</a> ';

                        var dy = '<a class="btn btn-success btn-sm ' + s_edit_h + '" href="#" mce_href="#" title="编辑" onclick="editCommander(\''
                            + value + '\')">' + '<i class="fa fa-edit"></i>编辑</a> ';

                        var d = '<a class="btn btn-success btn-sm ' + s_closeSet_h + '" href="#" mce_href="#" title="打烊设置" onclick="closeSet(\''
                            + value + '\')">' + '<i class="fa fa-wrench"></i>打烊设置</a> ';

                        var l = '<a class="btn btn-success btn-sm ' + s_teamuser_h + '" href="#" mce_href="#" title="查看门店用户" onclick="toTeamUser(\''
                            + value + '\')">' + '<i class="fa fa-wrench"></i>查看门店用户</a> ';

                        var r = '<a class="btn btn-success btn-sm ' + s_checkTimeSet_h + '" href="#" mce_href="#" title="设置开通时间" onclick="checkTimeSet(\''
                            + value + '\')">' + '<i class="fa fa-wrench"></i>设置开通时间</a> ';

                        // var syd = '<a class="btn btn-success btn-sm ' + s_setStaff_h + '" href="#" mce_href="#" title="设置为员工店主" onclick="setStaff(\''
                        //     + value + '\')">' + '<i class="fa fa-wrench">设置为员工店主</i></a> ';

                        // var cyd = '<a class="btn btn-success btn-sm ' + s_closeStaff_h + '" href="#" mce_href="#" title="取消员工店主" onclick="colseStaff(\''
                        //     + value + '\')">' + '<i class="fa fa-wrench">取消员工店主</i></a> ';

                        var agent = '';
                        if (row.isAgent == 1) {
                            agent = '<a class="btn btn-primary btn-sm ' + s_agent_h + '" href="#" mce_href="#" title="取消代理" onclick="setAgent(\''
                                + value + '\',0)"><i class="fa fa-user"></i>取消商户代理</a> ';
                        } else {
                            agent = '<a class="btn btn-primary btn-sm ' + s_agent_h + '" href="#" mce_href="#" title="成为代理" onclick="setAgent(\''
                                + value + '\',1)"><i class="fa fa-user"></i>成为商户代理</a> ';
                        }
                        var useragent = "";
                        if (row.isUseragent == 1) {
                            useragent = '<a class="btn btn-primary btn-sm ' + s_useragent_h + '" href="#" mce_href="#" title="取消代理" onclick="setUseragent(\''
                                + value + '\',0)"><i class="fa fa-user"></i>取消用户代理</a> ';
                        } else {
                            useragent = '<a class="btn btn-primary btn-sm ' + s_useragent_h + '" href="#" mce_href="#" title="成为代理" onclick="setUseragent(\''
                                + value + '\',1)"><i class="fa fa-user"></i>成为用户代理</a> ';
                        }

                        var isFreecharge = '<a class="btn btn-primary btn-sm" href="#" mce_href="#" title="设置免单活动" onclick="addFreeChargeInfo(\''
                            + value + '\')"><i class="fa fa-user"></i>设置免单活动</a> ';
                        if (row.isFreecharge == 1 || row.isFreecharge == '1') {
                            isFreecharge = isFreecharge + '<a class="btn btn-primary btn-sm" href="#" mce_href="#" title="取消参与" onclick="cancelFreeCharge(\''
                                + value + '\')"><i class="fa fa-user"></i>取消参与免单</a> ';
                        }

                        var hmdString = '';
                        var hmdValue = '';
                        if (row.status == 9) {
                            hmdString = '移出黑名单';
                            hmdValue = '1';
                        } else {
                            hmdString = '加入黑名单';
                            hmdValue = '9';
                        }

                        if (row.tnum == 0) {
                            return e + dy + d + isFreecharge;
                        } else {
                            return e + dy + d + isFreecharge;
                        }
                    }
                }]
        });
}

function reLoad() {
    $('#commanderListTable').bootstrapTable('refresh');
}

function remove(centreId) {
    layer.confirm('确定要删除选中的记录？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/remove",
            type: "post",
            data: {
                'centreId': centreId
            },
            beforeSend: function (request) {
                index = layer.load();
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.close(index);
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}

function batchRemove() {
    var rows = $('#commanderListTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
    if (rows.length == 0) {
        layer.msg("请选择要删除的数据");
        return;
    }
    layer.confirm("确认要删除选中的'" + rows.length + "'条数据吗?", {
        btn: ['确定', '取消']
        // 按钮
    }, function () {
        var ids = new Array();
        // 遍历所有选择的行数据，取每条数据对应的ID
        $.each(rows, function (i, row) {
            ids[i] = row['centreId'];
        });
        $.ajax({
            type: 'POST',
            data: {
                "ids": ids
            },
            url: prefix + '/batchRemove',
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    }, function () {
    });
}

function setAgent(id, isAgent) {
    var prompt = "";
    if (isAgent == 1) {
        prompt = "确定设置该用户为代理吗？";
    } else {
        prompt = "确定取消该用户代理吗？";
    }
    layer.confirm(prompt, {
        btn: ['确定', '取消']
        // 按钮
    }, function () {
        //防止重复点击
        layer.open({type: 1, title: false, closeBtn: false});
        $.ajax({
            type: 'POST',
            data: {
                "id": id,
                "isAgent": isAgent
            },
            url: prefix + '/setAgent',
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                    //关闭所有弹窗
                    layer.closeAll();
                } else {
                    layer.msg(r.msg);
                    //关闭所有弹窗
                    layer.closeAll();
                }
            }
        });
    }, function () {
    });
}

function add() {
    // iframe层
    layer.open({
        id: 'centre_add',
        type: 2,
        title: '添加商户',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['60%', '80%'],
        content: prefix + '/add' // iframe的url
    });
}

function cancelCommander(id) {
    layer.open({
        id: 'commander_cancel',
        type: 2,
        title: '取消门店',
        maxmin: true,
        shadeClose: false,
        area: ['30%', '40%'],
        content: prefix + '/cancelCommander/' + id
    });
}

function closeSet(id) {
    layer.open({
        id: 'close_set',
        type: 2,
        title: '打烊设置',
        maxmin: true,
        shadeClose: false,
        area: ['30%', '50%'],
        content: prefix + '/closeSet/' + id
    });
}

function setUseragent(id, isUseragent) {
    var prompt = "";
    if (isUseragent == 1) {
        prompt = "确定设置该用户为代理吗？";
    } else {
        prompt = "确定取消该用户代理吗？";
    }
    layer.confirm(prompt, {
        btn: ['确定', '取消']
        // 按钮
    }, function () {
        //防止重复点击
        layer.open({type: 1, title: false, closeBtn: false});
        $.ajax({
            type: 'POST',
            data: {
                "id": id,
                "isUseragent": isUseragent
            },
            url: '/member/manage/update',
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                    //关闭所有弹窗
                    layer.closeAll();
                } else {
                    layer.msg(r.msg);
                    //关闭所有弹窗
                    layer.closeAll();
                }
            }
        });
    }, function () {
    });
}

function cancelFreeCharge(id) {
    var prompt = "确定取消该门店的免单活动吗？";
    layer.confirm(prompt, {
        btn: ['确定', '取消']
        // 按钮
    }, function () {
        //防止重复点击
        layer.open({type: 1, title: false, closeBtn: false});
        $.ajax({
            type: 'POST',
            data: {
                "id": id,
                isFreecharge: 0
            },
            url: '/member/manage/update2',
            success: function (r) {
                if (r.code == 0) {
                    layer.msg(r.msg);
                    reLoad();
                    //关闭所有弹窗
                    layer.closeAll();
                } else {
                    layer.msg(r.msg);
                    //关闭所有弹窗
                    layer.closeAll();
                }
            }
        });
    }, function () {
    });
}

function addFreeChargeInfo(id) {
    // iframe层
    layer.open({
        id: 'activity_add',
        type: 2,
        title: '新增活动',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['100%', '100%'],
        content: prefix + '/toFreeChargeInfoAdd?mid=' + id// iframe的url
    });
}

function toTeamUser(id) {
    layer.open({
        id: 'close_set',
        type: 2,
        title: '门店用户',
        maxmin: true,
        shadeClose: false,
        area: ['60%', '80%'],
        content: prefix + '/toTeamUser/' + id
    });
}

function checkTimeSet(id) {
    layer.open({
        id: 'check_time_set',
        type: 2,
        title: '设置开通时间',
        maxmin: true,
        shadeClose: false,
        area: ['30%', '50%'],
        content: prefix + '/checkTimeSet/' + id
    });
}

function editCommander(id) {
    layer.open({
        id: 'commander_edit',
        type: 2,
        title: '门店编辑',
        maxmin: true,
        shadeClose: false,
        area: ['60%', '80%'],
        content: prefix + '/edit/' + id
    });
}

/**
 * 门店列表导出excel
 */
function exportCommander() {
    var follow = $.trim($("#follow").val());
    var startDate = $.trim($("#startDate").val());
    var endDate = $.trim($("#endDate").val());
    var nickname = $.trim($("#nickname").val());
    var mobile = $.trim($("#mobile").val());
    var id = $.trim($("#id").val());
    //var centreId = $("*[name='centreId']").val();
    var centreId = $.trim($("#centreId").val());
    window.location.href = prefix + "/exportCommander?follow=" + follow + "&startDate=" + startDate + "&endDate=" + endDate + "&nickname=" + nickname + "&mobile=" + mobile + "&id=" + id + "&centreId=" + centreId;
}

function orderList(id) {
    layer.open({
        id: 'commander_orderList',
        type: 2,
        title: '订单列表',
        maxmin: true,
        shadeClose: false,
        area: ['90%', '80%'],
        content: '/member/manage/getOrderList/' + id
    });
}

function updateBondmoney(id) {
    layer.open({
        id: 'commander_updateBondmoney',
        type: 2,
        title: '修改保证金',
        maxmin: true,
        shadeClose: false,
        area: ['40%', '55%'],
        content: '/member/manage/updateBondmoney/' + id
    });
}

function hmd(id, status) {
    var str = '';
    if (status == 9) {
        str = '确定添加黑名单吗？';
    } else {
        str = '确定移出黑名单吗？';
    }
    layer.confirm(str, {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: "/member/manage/hmd",
            type: "post",
            data: {
                'id': id,
                'status': status
            },
            beforeSend: function (request) {
                index = layer.load();
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.close(index);
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}


function colseStaff(id) {
    layer.confirm("确定关闭员工店主", {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: "/commanderList/manage/setStaff",
            type: "post",
            data: {
                'id': id,
                'tnum': 0
            },
            beforeSend: function (request) {
                index = layer.load();
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.close(index);
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}


function setStaff(id) {
    layer.confirm("确定设置员工店主", {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: "/commanderList/manage/setStaff",
            type: "post",
            data: {
                'id': id,
                'tnum': 1
            },
            beforeSend: function (request) {
                index = layer.load();
            },
            success: function (r) {
                if (r.code == 0) {
                    layer.close(index);
                    layer.msg(r.msg);
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    })
}

function bondFlow(id) {
    layer.open({
        id: 'commander_bondFlow',
        type: 2,
        title: '保证金流水',
        maxmin: true,
        shadeClose: false,
        area: ['60%', '80%'],
        content: '/flow/bond/bondFlowToMid/' + id
    });
}

function balanceFlow(id) {
    layer.open({
        id: 'commander_balanceFlow',
        type: 2,
        title: '余额流水',
        maxmin: true,
        shadeClose: false,
        area: ['60%', '80%'],
        content: '/flow/balance/balanceFlowToMid/' + id
    });
}

function withdrawals(id) {
    layer.open({
        id: 'commander_withdrawals',
        type: 2,
        title: '提现记录',
        maxmin: true,
        shadeClose: false,
        area: ['60%', '90%'],
        content: '/finance/withdrawals/withdrawalsToMid/' + id
    });
}

function achieveFlow(id) {
    layer.open({
        id: 'commander_achieveFlow',
        type: 2,
        title: '业绩流水',
        maxmin: true,
        shadeClose: false,
        area: ['60%', '90%'],
        content: '/flow/achieve/achieveFlowsToMid/' + id
    });
}

function commissionLog(id) {
    layer.open({
        id: 'commander_commissionLog',
        type: 2,
        title: '返点记录',
        maxmin: true,
        shadeClose: false,
        area: ['80%', '90%'],
        content: '/finance/commission/commissionLogToMid/' + id
    });
}